/*
 * Copyright (c) 2009-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/media/Image.h"

namespace alkes {

Image::Image()
: buffer_(0)
, x_(0), y_(0)
, width_(0), height_(0)
{
}

Image::Image(ImageBuffer* buffer)
: buffer_(buffer)
, x_(0), y_(0)
, width_(0), height_(0)
{
    if (buffer)
    {
        buffer->getImageSize(width_, height_);
    }
}

Image::Image(ImageBuffer* buffer, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
: buffer_(buffer)
, x_(x), y_(y)
, width_(width), height_(height)
{
}

Image::Image(const Image& rh)
: buffer_(rh.getImageBuffer())
, x_(rh.x_)
, y_(rh.y_)
, width_(rh.width_)
, height_(rh.height_)
{
}

void Image::operator=(const Image& rh)
{
    buffer_ = rh.getImageBuffer();
    x_ = rh.x_;
    y_ = rh.y_;
    width_ = rh.width_;
    height_ = rh.height_;
}

Image::~Image()
{
}

Image::operator bool() const
{
    return 0 != buffer_;
}

void Image::setImageBuffer(ImageBuffer* buffer)
{
    uint16_t w, h;

    if (buffer)
        buffer->getImageSize(w, h);
    else
        w = h = 0;

    setImageBuffer(buffer, 0, 0, w, h);
}

void Image::setImageBuffer(ImageBuffer* buffer, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
{
    buffer_ = buffer;
    x_ = x;
    y_ = y;
    width_ = width;
    height_ = height;
}

ImageBuffer* Image::getImageBuffer() const
{
    return buffer_;
}

PixelFormat Image::getPixelFormat() const
{
    if (buffer_)
        return buffer_->getPixelFormat();
    else
        return AL_PIXEL_FORMAT_UNKNOWN;
}

void Image::getOffset(uint16_t& x, uint16_t& y) const
{
    if (buffer_)
    {
        x = x_;
        y = y_;
    }
    else
    {
        x = 0;
        y = 0;
    }
}

void Image::setOffset(uint16_t x, uint16_t y)
{
    x_ = x;
    y_ = y;
}

void Image::getSize(uint16_t& width, uint16_t& height) const
{
    if (buffer_)
    {
        width = width_;
        height = height_;
    }
    else
    {
        width = 0;
        height = 0;
    }
}

void Image::setSize(uint16_t width, uint16_t height)
{
    width_ = width;
    height_ = height;
}

const Rect Image::getRect() const
{
    uint16_t x, y, w, h;

    getOffset(x, y);
    getSize(w, h);

    Rect rect = {x, y, w, h};

    return rect;
}

}
